package cn.wx.order.service.impl;

import cn.wx.basic.service.impl.BaseServiceImpl;
import cn.wx.order.domain.OrderPetAcquisition;
import cn.wx.order.dto.ConfirmDto;
import cn.wx.order.mapper.OrderPetAcquisitionMapper;
import cn.wx.order.service.IOrderPetAcquisitionService;
import cn.wx.pet.domain.Pet;
import cn.wx.pet.mapper.PetMapper;
import cn.wx.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class OrderPetAcquisitionService extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;
    /*
    *   订单接受
    * */
    @Override
    public void ConfirmDto(ConfirmDto dto) {
        Long id = dto.getId();
        BigDecimal money = dto.getMoney();
        Integer payType = dto.getPayType();

        /**
         * 1. 改订单状态，金额, 支付方式
         * 2. 改寻主消息状态
         * 3. 改pet金额
         */
        // 1.改订单状态，金额, 支付方式
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        orderPetAcquisition.setPrice(money);
        orderPetAcquisition.setState(2);
        orderPetAcquisition.setPaytype(payType);
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        //修改寻主状态
        searchMasterMsgMapper.updateState(orderPetAcquisition.getSearch_master_msg_id());

        //改pet金额
        Pet pet = petMapper.loadById(orderPetAcquisition.getPet_id());
        pet.setCostprice(money);
        petMapper.update(pet);
    }


    /*
    *   取消订单
    * */
    @Override
    public void cancle(Long id) {
        //修改状态
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 2. 寻主消息的shopid=null
        searchMasterMsgMapper.reject(id);

        // 3. pet 删除
        petMapper.remove(orderPetAcquisition.getPet_id());
    }


}
